﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.Auditing;
using Abp.Web.Models;
using FwProject.Controllers;
using FwProject.Product;
using FwProject.Product.Dto;
using Microsoft.AspNetCore.Mvc;

namespace FwProject.Web.Shop.Controllers
{
    /// <summary>
    /// 类型管理
    /// </summary>
    public class GenreController : FwProjectControllerBase
    {
        private readonly IProductTypeAppService _productTypeAppService;
        private readonly IProductTypeAttrAppService _productTypeAttrAppService;
        private readonly IProductTypeParAppService _productTypeParAppService;
        private readonly IProductTypeBrandAppService _productTypeBrandAppService;
        private readonly IProductTypeSpceAppService _productTypeSpceAppService;
        public GenreController(IProductTypeAppService productTypeAppService, IProductTypeAttrAppService productTypeAttrAppService, IProductTypeParAppService productTypeParAppService, IProductTypeBrandAppService productTypeBrandAppService, IProductTypeSpceAppService productTypeSpceAppService)
        {
            _productTypeAppService = productTypeAppService;
            _productTypeAttrAppService = productTypeAttrAppService;
            _productTypeParAppService = productTypeParAppService;
            _productTypeBrandAppService = productTypeBrandAppService;
            _productTypeSpceAppService = productTypeSpceAppService;
        }
        public IActionResult Index()
        {
            return View();
        }

        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]//关闭ABP的返回数据封装
        public async Task<IActionResult> GetGenreData(int page, int limit, string keyword = "")
        {
            var data = await _productTypeAppService.GetLayerDataAsync(page, limit, keyword);
            return Json(data);
        }


        [DisableAuditing]
        public IActionResult Create()
        {
            return View();
        }

        public async Task<IActionResult> CreateProductType(ProductTypeInput input)
        {
            var id = await _productTypeAppService.CreateAsync(input);
            return Json(id);
        }
        [DisableAuditing]
        public async Task<IActionResult> Edit(Guid id)
        {
            var entity = await _productTypeAppService.GetEnterDtoAsync(id);

            return View(entity);
        }
        public async Task<IActionResult> ModifyProductType(Guid id, ProductTypeInput input)
        {
            var uid = await _productTypeAppService.ModifyAsync(id, input);
            return Json("");
        }

        [HttpGet]
        public async Task<IActionResult> Delete(Guid id)
        {
            await _productTypeAppService.DeleteAsync(id);
            return Json(id);
        }

        #region 属性
        public async Task<IActionResult> EditAttr(Guid id)
        {
            ViewBag.Id = id;
            var dto = await _productTypeAttrAppService.GetEnterDtoAsync(id);
            return View(dto);
        }

        [HttpPost]
        public async Task<IActionResult> UpdateAttr(Guid id, ProductTypeAttrEnterDto input)
        {
            await _productTypeAttrAppService.UpdatedBatchAsync(id, input);
            return Json(id);

        }
        #endregion



        public async Task<IActionResult> EditPar(Guid id)
        {
            ViewBag.Id = id;
            var dto =  await _productTypeParAppService.GetEnterDtoAsync(id);
            return View(dto);
        }

        [HttpPost]
        public async Task<IActionResult> UpdatePar(Guid id, ProductTypeUpdateDto input)
        {
            await _productTypeParAppService.UpdatedBatchAsync(id, input);
            return Json(id);

        }


        #region 关联品牌
        public IActionResult AssociatedBrand(Guid id)
        {
            ViewBag.Id = id;
            var list = _productTypeBrandAppService.GetOutputDto(id);
            return View(list);
        }

        [HttpPost]
        public async Task<IActionResult> UpdateBrand(Guid id, ProductTypeBrandEnterDto input)
        {
            await _productTypeBrandAppService.UpdatedBatchAsync(id, input);
            return Json(id);

        }
        #endregion
        /// <summary>
        /// 关联规格
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult AssociatedSpce(Guid id)
        {
            ViewBag.Id = id;
            var list = _productTypeSpceAppService.GetOutputDto(id);
            return View(list);
        }

        [HttpPost]
        public async Task<IActionResult> UpdateSpce(Guid id, ProductTypeSpceEnterDto input)
        {
            await _productTypeSpceAppService.UpdatedBatchAsync(id, input);
            return Json(id);

        }

        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]//关闭ABP的返回数据封装
        public IActionResult GetSearchSuggest(string keyword="")
        {
            var data = _productTypeAppService.GetSearchSuggestData(keyword);
            return Json(data);
        }

    }
}